<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\common\repositories\property\basic;

use think\facade\Db;
use app\traits\CategoryRepository;
use app\common\repositories\BaseRepository;
use app\common\dao\property\basic\PropertyUnitDao;
use app\common\repositories\room\RoomBasicRepository;
use app\common\repositories\property\users\PropertyUsersLogRepository;

/**
 * Class PropertyUnitRepository
 *
 * @mixin PropertyUnitDao
 */
class PropertyUnitRepository extends BaseRepository
{

    use CategoryRepository;

    public function __construct(PropertyUnitDao $dao)
    {
        $this->dao = $dao;
    }


    public function getPropertyList(array $where, $page, $limit, int $propertyId = null)
    {
        $query = $this->dao->search($where, $propertyId);
        $count = $query->count();
        $list = $query->page($page, $limit)
            ->with([
                'propertyInfo' => function ($query) {
                    $query->bind(['property_name' => 'name']);
                },
            ])
            ->append(['full_unit_name'])
            ->order('id', 'desc')
            ->select();

        return compact('count', 'list');
    }

    /**
     *  获取全部楼栋
     * @param array $where
     * @param $propertyId
     * @return array
     *
     * @author Amos
     * 2024-06-12 22:17
     */
    public function getApiFormatList(array $where, $propertyId = null)
    {
        return formatCategory($this->dao->getAll($where, $propertyId, 'id,unit_name,pid')
            ->toArray(), $this->dao->getPk());
    }

    public function addInfo(array $data, int $userId = 0)
    {
        return Db::transaction(function () use ($data, $userId) {
            $res = $this->create($data);
            /** @var PropertyUsersLogRepository $repository */
            $repository = app()->make(PropertyUsersLogRepository::class);
            $repository->addInfo($data['property_id'], $userId, 'PROPERTY.UNIT.ADD', $data);
            return $res;
        });
    }

    public function editInfo($info, array $data, int $userId = 0)
    {
        return Db::transaction(function () use ($data, $info, $userId) {
            /** @var PropertyUsersLogRepository $repository */
            $repository = app()->make(PropertyUsersLogRepository::class);
            $repository->addInfo($info['property_id'], $userId, 'PROPERTY.UNIT.EDIT', $data);
            return $this->dao->update($info['id'], $data);
        });
    }


    public function getDetail(int $id)
    {
        $data = $this->dao->search([])
            ->where('id', $id)
            ->append(['full_unit_name'])
            ->find();
        return $data;
    }

    public function batchDelete(array $ids, int $userId = 0)
    {
        $list = $this->dao->selectWhere([
            ['id', 'in', $ids]
        ]);
        if ($list) {
            foreach ($list as $k => $v) {
                $this->deleteInfo($v, $userId);
            }
            return $list;
        }
        return [];
    }

    public function deleteInfo($info, int $userId = 0)
    {
        return Db::transaction(function () use ($info, $userId) {

            ## 同步删除当前单元下所有的房屋
            $roomBasicRepository = app()->make(RoomBasicRepository::class);
            $roomBasicRepository->getSearch(['unit_id' => $info['id']])->delete();

            /** @var PropertyUsersLogRepository $repository */
            $repository = app()->make(PropertyUsersLogRepository::class);
            $repository->addInfo($info['property_id'], $userId, 'PROPERTY.UNIT.DEL', $info);
            return $this->dao->delete($info['id']);
        });
    }

    public function deleteCheckRoom(int $id)
    {
        /** @var RoomBasicRepository $roomBasicRepository */
        $roomBasicRepository = app()->make(RoomBasicRepository::class);
        $rentRoomNum = $roomBasicRepository->getSearch(['unit_id' => $id])
            ->where('status', 2)
            ->count();
        if ($rentRoomNum > 0) {
            return '存在已出租的房间，请先处理';
        }
        return true;
    }

}